const app = getApp()
const db = wx.cloud.database()

module.exports = {
  methods: {
    /**
     * 显示登陆模态框
     */
    showModal() {
      this.setData({
        modalShow: true
      })
    },
    /**
     * 点击模态框的取消
     */
    clickModalCancel(e) {
      this.setData({
        modalShow: false,
      })
    },
    /**
     * 获取用户登录信息
     */
    getProfile() {
      if (app.global_data.user_data.user_info) {
        this.setData({
          user: app.global_data.user,
          user_data: app.global_data.user_data
        })
      } else {
        wx.getUserInfo({
          success: function (res) {
            let user_info = res.userInfo
            app.global_data.user = user_info
            wx.setStorageSync('user', user_info)
            wx.cloud.callFunction({
              name: 'login',
              data: {
                user_info,
                openid:''
              },
              success: res => {
                console.log('获取登录信息成功', res.result.user_data)
                let user_data = res.result.user_data[0]
                app.global_data.user_data = user_data
                wx.setStorageSync('user_data', user_data)
              },
              fail: err => {
                console.error('获取登录信息失败', err)
              }
            })
          }
        })
      }
    },
  }
}